//
//  GongHaiFenPeiViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/20.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "GongHaiFenPeiViewController.h"
#import "KeHuLeiXingShaiXuanTableViewCell.h"
#import "ZiDingYiXuanChengYuanViewController.h"
@interface GongHaiFenPeiViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSString *currentIndex;
    NSArray *dataSource;
    
    NSDictionary *yuanGongDic;
}

@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *mainScrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSpace;

@property (weak, nonatomic) IBOutlet UIView *quanXianView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *mainTableView;
@property (weak, nonatomic) IBOutlet UILabel *remindLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeight;
@property (weak, nonatomic) IBOutlet UITextField *firstValueTf;
@property (weak, nonatomic) IBOutlet UITextField *quanXianTf;
@property (weak, nonatomic) IBOutlet UIButton *xuanZeButton;
@end

@implementation GongHaiFenPeiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    
    if(_currentType == 0){
        self.customNavBar.title = @"分配";
    }else  if(_currentType == 1){
        self.customNavBar.title = @"放入公海";
        _remindLabel.text = @"公海名称";
        [self huoQuGongHaiList];
    }else  if(_currentType == 2){
        self.customNavBar.title = @"共享";
        _remindLabel.text = @"选择团队成员";
        _quanXianView.hidden = NO;
    }else  if(_currentType == 4){
        self.customNavBar.title = @"标记无效";
        _remindLabel.text = @"公海名称";
        [self huoQuGongHaiList];
    }
    
    if (@available(iOS 11.0, *)) {
        self.mainScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    _topSpace.constant = NavaBarHeight;
    
    _bottomSpace.constant = 20+ScreenBottom;
    
    if (@available(iOS 11.0, *)) {
        self.mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"KeHuLeiXingShaiXuanTableViewCell" bundle:nil] forCellReuseIdentifier:@"KeHuLeiXingShaiXuanTableViewCell"];
    
    
    
    _mainTableView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1600].CGColor;
    _mainTableView.layer.shadowOffset = CGSizeMake(2.5,2.5);
    _mainTableView.layer.shadowOpacity = 1;
    _mainTableView.layer.shadowRadius = 5;
    

    
}

//查询可选择的公海列表
- (void)huoQuGongHaiList{
    [[ZZPRequest getWebUtil] postNetworkRequestURLString:@"/crmCustomerPool/queryPoolNameListByAuth" andParas:nil andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            dataSource = [info objectForKey:@"data"];
            [_mainTableView reloadData];
        
            if(dataSource.count*48 > ScreenHeight-109-44-ScreenBottom-20-20-NavaBarHeight){
                _tableViewHeight.constant = ScreenHeight-109-44-ScreenBottom-20-20-NavaBarHeight;
            }else{
                _tableViewHeight.constant = dataSource.count*48;
            }
        }
    }];
}
//选择
- (IBAction)xuanZe:(UITapGestureRecognizer *)sender {

    if(_currentType == 2 || _currentType == 3 || _currentType == 0){
//        共享
        ZiDingYiXuanChengYuanViewController *ziDingYi = [[ZiDingYiXuanChengYuanViewController alloc] init];
        ziDingYi.type = 2;
        ziDingYi.manageResultBlock = ^(NSArray * _Nonnull yuanGongArray, NSArray * _Nonnull buMenArray) {
            if(yuanGongArray.count > 0){
                yuanGongDic = yuanGongArray.firstObject;
                _firstValueTf.text = GetHandleString(yuanGongDic, @"realname");
            }
        };
        [self pushViewController:ziDingYi];
        return;
   }
   
    _xuanZeButton.selected = !_xuanZeButton.selected;
    _mainTableView.hidden = !_xuanZeButton.selected;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KeHuLeiXingShaiXuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeHuLeiXingShaiXuanTableViewCell"];
    cell.itemLabel.text = @"赵宇";
    NSDictionary *dataDic = dataSource[indexPath.row];
    
    if(_currentType == 0){
//    分配
    }else  if(_currentType == 1 || _currentType == 4){
//        放入公海
        cell.itemLabel.text = GetHandleString(dataDic, @"poolName");
    }else  if(_currentType == 2){
//        共享
        _quanXianView.hidden = NO;
    }
    
    if(currentIndex.integerValue == indexPath.row && currentIndex.length > 0){
        cell.itemLabel.textColor = rgba(40, 120, 255, 1);
    }else{
        cell.itemLabel.textColor = rgba(90, 98, 112, 1);
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    currentIndex = [NSString stringWithFormat:@"%ld",indexPath.row];
    [_mainTableView reloadData];
    NSDictionary *dataDic = dataSource[indexPath.row];
    if(_currentType == 0){
//    分配
    }else  if(_currentType == 1 || _currentType == 4){
//        放入公海
    _firstValueTf.text = GetHandleString(dataDic, @"poolName");
    }else  if(_currentType == 2){
//        共享
        _quanXianView.hidden = NO;
    }
    _mainTableView.hidden = YES;
}
- (IBAction)queDing:(UIButton *)sender {
    if(_currentType == 0){
//    分配
        if(_firstValueTf.text.length == 0){
            [ZZPUtils showWarning:@"请先选择负责人"];
            return;
        }
        NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
        
        [paraDic setValue:_poolId forKey:@"poolId"];
        
        [paraDic setValue:@[_customerId] forKey:@"ids"];
        
        [paraDic setValue:GetHandleString(yuanGongDic, @"userId") forKey:@"userId"];
        
        
        
        [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/crmCustomer/distributeByIds" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
            if(!error){
                [ZZPUtils showWarning:@"分配成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
        
        
    }else  if(_currentType == 1){
//        放入公海
        if(_firstValueTf.text.length == 0){
            [ZZPUtils showWarning:@"请先选择公海名称"];
            return;
        }

        NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
        
        [paraDic setValue:GetHandleString(dataSource[[currentIndex integerValue]], @"poolId") forKey:@"poolId"];
        
        [paraDic setValue:@[_customerId] forKey:@"ids"];
        
        
        [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/crmCustomer/updateCustomerByIds" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
            if(!error){
                [ZZPUtils showWarning:@"放入成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    
    }else if(_currentType == 2){
//        共享
        if(_firstValueTf.text.length == 0){
            [ZZPUtils showWarning:@"请先选择团队成员"];
            return;
        }
        
        NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
        
        [paraDic setValue:@"1" forKey:@"power"];
        
        [paraDic setValue:@[_customerId] forKey:@"ids"];
        
        [paraDic setValue:@[GetHandleString(yuanGongDic, @"userId")] forKey:@"memberIds"];
        
        [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/crmCustomer/addMembers" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
            if(!error){
                [ZZPUtils showWarning:@"共享成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }else if(_currentType == 3){
        //        转移
        if(_firstValueTf.text.length == 0){
            [ZZPUtils showWarning:@"请先选择团队成员"];
            return;
        }
        
        NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
        
        [paraDic setValue:@"1" forKey:@"transferType"];
        
        [paraDic setValue:@[_customerId] forKey:@"ids"];
        
        [paraDic setValue:GetHandleString(yuanGongDic, @"userId") forKey:@"ownerUserId"];
        [paraDic setValue:[NSArray array] forKey:@"changeType"];
        [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/crmCustomer/changeOwnerUser" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
            if(!error){
                [ZZPUtils showWarning:@"转移成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }else if(_currentType == 4){
        //        标记无效
        if(_firstValueTf.text.length == 0){
            [ZZPUtils showWarning:@"请先选择公海名称"];
            return;
        }
        
        NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
        
        [paraDic setValue:GetHandleString(dataSource[[currentIndex integerValue]], @"poolId") forKey:@"poolId"];
        
        [paraDic setValue:@[_customerId] forKey:@"ids"];
        
        [paraDic setValue:@"1" forKey:@"isEffective"];
        [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/crmCustomer/updateCustomerByIds" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
            if(!error){
                [ZZPUtils showWarning:@"已标记"];
                [self.navigationController popViewControllerAnimated:YES];
                if (_manageResultBlock) {
                    _manageResultBlock(@"",@"");
                }
            }
        }];
    }
    

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

